﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using rbac.contracts.Dto.Input;
using rbac.contracts.Dto.Output;
using rbac.contracts.Interface;

namespace rbac.api.Controllers
{
    /// <summary>
    /// 用户管理123456
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly TokenService _tokenService;
        private readonly ILogger<UserController> _logger;
        
        
        public UserController(IUserService userService
            , TokenService tokenService
            , ILogger<UserController> logger)
        {
            _userService = userService;
            _tokenService = tokenService;
            _logger = logger;
        }

        /// <summary>
        /// 用户查询。。。
        /// </summary>
        /// <remarks>用户查询的详细说明</remarks>
        /// <returns></returns>
        [HttpPost] // 请求谓词
        public Results<PageOutput<UserOutput>> GetUsers([FromBody]GetUsersInput input)
        {
            return Results<PageOutput<UserOutput>>.DataResult(_userService.GetUsers(input));
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input">用户名与密码</param>
        /// <returns></returns>
        [HttpPost]
        public Results<UserOutput> Login([FromBody] LoginInput input)
        {
            var userDto = _userService.Login(input);
            if (userDto != null)
            {
                // _logger.LogInformation($"{userDto.NickName}登录了");
                var token = _tokenService.GenerateToken(userDto);
                return Results<UserOutput>.TokenResult(token,userDto);
            }
            _logger.LogError($"账号：{input.UserName}，密码：{input.Pwd}:登录失败：");
            return Results<UserOutput>.FailResult("用户或者密码不正确");
        }
    }
}
